﻿var provinceName;
var cityName;
var arrCity;
var areaName;
var arrArea;
var actionUrl = "../../sys/ashx/UserHandler.ashx?action=myusercbd";
var filterObj;
var filterObj1;
var dtype;
var dtypecn;
var ulevel;
var lv;
var uname;
var uid;
var cid = "";
var hDialog;
var devid = "0";
function getArgs(name) {
    var args = {};
    var match = null;
    var search = decodeURIComponent(location.search.substring(1));
    var reg = /(?:([^&]+)=([^&]+))/g;
    while ((match = reg.exec(search)) !== null) {
        args[match[1]] = match[2];
    }
    return args;
    //return search;
}
function getUrlParam(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
    var r = window.location.search.substr(1).match(reg);  //匹配目标参数
    if (r != null) return unescape(r[2]); return null; //返回参数值
}

$(function () {

    var size = { width: $(window).width(), height: $(window).height() };
    mylayout.init(size);
    
    areatree.init();
    //autoResize({ dataGrid: '#histList', gridType: 'datagrid', callback: grid.bind, height:80});
    
    mygrid.databind(size, filterObj);
    $(window).resize(function () {
        size = { width: $(window).width(), height: $(window).height() };
        mylayout.resize(size);
    });
    $('#dtOpstart,#dtOpend').datebox({ width: 100 });
});
var mylayout = {
    init: function (size) {
        $('#layout').width(size.width - 4).height(size.height - 4).layout();
        var center = $('#mylayout').layout('panel', 'center');
        grid.bind(center.size);
        center.panel({
            onResize: function (w, h) {

                //$('#userGrid').datagrid('resize', { width: w, height: h });
                $('#histList').datagrid('resize', { width: w, height: h });
            }
        });
    },
    resize: function (size) {
        mylayout.init(size);
        $('#layout').layout('resize');
    }
};
var areatree = {
    init: function () {
        $('#deptree').tree({
            lines: true,
            url: '/ashx/AreaHandler.ashx?isadd=1',
            animate: true,
            onLoadSuccess: function (node, data) {
                if (node == null)
                    $('body').data('areaData', data);
            },
            onBeforeExpand: function (node) {
                $('#deptree').tree('options').url = "/ashx/AreaHandler.ashx?keyid=" + node.id;
            },
            onClick: function (node) {

                if (node.attributes != null) {
                    lv = node.attributes.ulevel;
                    ulevel = node.id;
                    uname = $("#uname").val();
                    getopt();
                }
                else {
                    filterObj = "";
                }
                $('#userGrid').datagrid('load', { filter: JSON.stringify(filterObj) });
            }
        });
    },
    data: function () {
        var d = JSON.stringify($('body').data('areaData'));
        return JSON.parse(d);
    }

};
function searchlist()
{
     var ruleArr = [];
                dtOpstart = $('#dtOpstart').datebox('getValue');
                dtOpend = $('#dtOpend').datebox('getValue');
                if (cid != "") {
                    ruleArr.push({ "field": "devID", "op": "eq", "data": cid });
                    if (dtOpstart != '')
                        ruleArr.push({ "field": "updateTime", "op": "ge", "data": dtOpstart });
                    if (dtOpend != '')
                        ruleArr.push({ "field": "updateTime", "op": "le", "data": dtOpend });
                    if (ruleArr.length > 0) {
                        filterObj1 = { groupOp: 'AND', rules: ruleArr };
                        $('body').data('where1', JSON.stringify(filterObj1));
                        $('#histList').datagrid({
                            //title: "设备[" + rowData.username + "] 的历史记录",
                            url: '/status_cbd/ashx/DataTestCBDHandler.ashx?Action=detail',
                            queryParams: { devID: cid, filter: JSON.stringify(filterObj1) }
                        });
                    }
                    else {

                        $('#histList').datagrid({
                            //title: "设备[" + rowData.username + "] 的历史记录",
                            url: '/status_cbd/ashx/DataTestCBDHandler.ashx?Action=detail',
                            queryParams: { devID: cid }
                        });
                    }
                }
                

}
var mygrid = {
    databind: function (size, opt) {
        $('#userGrid').datagrid({
            url: actionUrl,
            width: size.width-250,
            height: size.height,
            idField: 'KeyId',
            singleSelect: true,
            striped: true,
            rownumbers: true,
            sortName: "status",
            queryParams: {
                filter: JSON.stringify(filterObj)
            },
            onClickRow: function (rowIndex, rowData) {
                var cc = rowData.DevID;
                cid = cc;
                var ruleArr = [];
                dtOpstart = $('#dtOpstart').datebox('getValue');
                dtOpend = $('#dtOpend').datebox('getValue');
                if (cid != "") {
                    ruleArr.push({ "field": "devID", "op": "eq", "data": cid });
                }
                if (dtOpstart != '')
                    ruleArr.push({ "field": "updateTime", "op": "ge", "data": dtOpstart });
                if (dtOpend != '')
                    ruleArr.push({ "field": "updateTime", "op": "le", "data": dtOpend });
                if (ruleArr.length > 0) {
                    filterObj1 = { groupOp: 'AND', rules: ruleArr };
                    $('body').data('where1', JSON.stringify(filterObj1));
                }
                $('#histList').datagrid({
                    //title: "设备[" + rowData.username + "] 的历史记录",
                    url: '/status_cbd/ashx/DataTestCBDHandler.ashx?Action=detail',
                    queryParams: { devID: cc }
                });
            },
            columns: [[
                {
                    title: '地址', field: 'madd', width: 250
                },
                { title: '设备ID', field: 'DevID', width: 100, sortable: true },
                {
                    title: '设备情况', field: 'status', width: 80, sortable: true, formatter: function (v, d, i) {
                        if (v == "OK") return '<img src="/css/icon/32/bullet_green.png" />';
                        if (v == "ERO") return '<img src="/css/icon/32/bullet_red.png" />';
                    }
                },
                 {
                     title: '灯管', field: 'sta_ch0', width: 70, align: 'center',
                     formatter: function (v, d, i) {
                         if (d.sta_ch0 == "0")
                             return '<img src="/css/icon/32/bullet_red.png" />';
                         else
                             return '<img src="/css/icon/32/bullet_green.png" />';
                     }
                 },
                {
                    title: '加热1', field: 'sta_ch1', width: 70, align: 'center',
                    formatter: function (v, d, i) {
                        if (d.sta_ch1 == "0")
                            return '<img src="/css/icon/32/bullet_red.png" />';
                        else
                            return '<img src="/css/icon/32/bullet_green.png" />';
                    }
                },
                {
                    title: '加热2', field: 'sta_ch2', width: 70, align: 'center',
                    formatter: function (v, d, i) {
                        if (d.sta_ch2 == "0")
                            return '<img src="/css/icon/32/bullet_red.png" />';
                        else
                            return '<img src="/css/icon/32/bullet_green.png" />';
                    }
                },
                {
                    title: '翻板', field: 'sta_ch3', width: 70, align: 'center',
                    formatter: function (v, d, i) {
                        if (d.sta_ch3 == "0")
                            return '<img src="/css/icon/32/bullet_red.png" />';
                        else
                            return '<img src="/css/icon/32/bullet_green.png" />';
                    }
                },
                {
                    title: '接雨', field: 'sta_ch4', width: 70, align: 'center',
                    formatter: function (v, d, i) {
                        if (d.sta_ch4 == "0")
                            return '<img src="/css/icon/32/bullet_red.png" />';
                        else
                            return '<img src="/css/icon/32/bullet_green.png" />';
                    }
                },
                {
                    title: '圆盘', field: 'sta_ch5', width: 70, align: 'center',
                    formatter: function (v, d, i) {
                        if (d.sta_ch5 == "0")
                            return '<img src="/css/icon/32/bullet_red.png" />';
                        else
                            return '<img src="/css/icon/32/bullet_green.png" />';
                    }
                }
                //,
                //{
                //    title: '查看设备', field: 'v', width: 200, formatter: function (v, d, i) {
                //        return '<a href="#" onclick="top.addTab(\'测报灯' + d.UserName + '\',\'status_cbd/DataTestCBD.aspx?uid=' + d.KeyId + '&navid=45\', \'icon icon-server\');">测报灯</a>';
                //    }
                //}
            ]],
            pagination: true,
            pageSize: PAGESIZE
        });
    },
    reload: function () {
        $('#userGrid').datagrid('clearSelections').datagrid('reload');
    },
    selectRow: function () {
        return $('#userGrid').datagrid('getSelected');
    }
};
var grid = {
    bind: function (winSize) {
        $('#histList').datagrid({
            toolbar: '#toolbar1',
            width: winSize.width - 250,
            height: winSize.height,
            //width:'100%',
            //height: '100%',
            nowrap: true, //折行
            rownumbers: true, //行号
            striped: true, //隔行变色
            idField: 'keyid', //主键
            singleSelect:true, //单选
            //selectOnCheck: true,
            //checkOnSelect: true,
            onClickRow: function (rowIndex, rowData) {
                $("#cc").combobox({
                    url: '/status_cbd/ashx/DataTestCBDHandler.ashx?Action=getUserRoleName',
                    valueField: 'KeyId',
                    textField: 'RoleName'
                });
                $("#cc").combobox({
                    onLoadSuccess: function (data) {
                        var selectData = $("#cc").combobox('getData');
                        if (selectData != null) {
                            $("#cc ").combobox('select', selectData[0].KeyId);
                        }

                    },
                    onSelect: function () {
                        $('#cct').combobox({
                            url: '/status_cbd/ashx/DataTestCBDHandler.ashx?Action=getUserInfo&roleId='
                                + $('#cc').combobox('getValue'),
                            valueField: 'UserID',
                            textField: 'TrueName',
                            multiple: "true"
                        });

                    }
                });

                var checkedStr = "";
                var d = rowData;
                    
                    if (d.sta_ch0 == "0") {
                        checkedStr += "灯管、";
                    }
                    if (d.sta_ch1 == "0") {
                        checkedStr += "加热1、";
                    }
                    if (d.sta_ch2 == "0") {
                        checkedStr += "加热2、";
                    }
                    if (d.sta_ch3 == "0") {
                        checkedStr += "翻板、";
                    }
                    if (d.sta_ch4 == "0") {
                        checkedStr += "接雨、";
                    }
                    if (d.sta_ch5 == "0") {
                        checkedStr += "圆盘、";
                    }
                    if (checkedStr != "") {
                        checkedStr = checkedStr.substring(0, checkedStr.lastIndexOf("、"));
                        checkedStr = "设备ID：" + d.devID+ checkedStr;
                        checkedStr += "工作不正常，时间：" + d.updateTime;
                        devid = d.devID;
                        $('#sendcontent').val(checkedStr);
                    }
               
            },
            columns: [[               
                { title: '设备ID', field: 'devID', width: 100, align: 'center' },
                {
                    title: '灯管', field: 'sta_ch0', width: 70, align: 'center',
                    formatter: function (v, d, i) {
                        if (d.sta_ch0 == "0")
                            return '<img src="/css/icon/32/bullet_red.png" />';
                        else
                            return '<img src="/css/icon/32/bullet_green.png" />';
                    }
                },
                {
                    title: '加热1', field: 'sta_ch1', width: 70, align: 'center',
                    formatter: function (v, d, i) {
                        if (d.sta_ch1 == "0")
                            return '<img src="/css/icon/32/bullet_red.png" />';
                        else
                            return '<img src="/css/icon/32/bullet_green.png" />';
                    }
                },
                {
                    title: '加热2', field: 'sta_ch2', width: 70, align: 'center',
                    formatter: function (v, d, i) {
                        if (d.sta_ch2 == "0")
                            return '<img src="/css/icon/32/bullet_red.png" />';
                        else
                            return '<img src="/css/icon/32/bullet_green.png" />';
                    }
                },
                {
                    title: '翻板', field: 'sta_ch3', width: 70, align: 'center',
                    formatter: function (v, d, i) {
                        if (d.sta_ch3 == "0")
                            return '<img src="/css/icon/32/bullet_red.png" />';
                        else
                            return '<img src="/css/icon/32/bullet_green.png" />';
                    }
                },
                {
                    title: '接雨', field: 'sta_ch4', width: 70, align: 'center',
                    formatter: function (v, d, i) {
                        if (d.sta_ch4 == "0")
                            return '<img src="/css/icon/32/bullet_red.png" />';
                        else
                            return '<img src="/css/icon/32/bullet_green.png" />';
                    }
                },
                {
                    title: '圆盘', field: 'sta_ch5', width: 70, align: 'center',
                    formatter: function (v, d, i) {
                        if (d.sta_ch5 == "0")
                            return '<img src="/css/icon/32/bullet_red.png" />';
                        else
                            return '<img src="/css/icon/32/bullet_green.png" />';
                    }
                },
                { title: '设备地点', field: 'DevLocation', width: 100, align: 'center' },
                { title: '所有人', field: 'DevOwnerName', width: 100, align: 'center' },
                { title: '更新时间', field: 'updateTime', width: 150, align: 'center' }
            ]],
            pagination: true,
            pageSize: PAGESIZE
        });
    },
    getSelectedRow: function () {
        return $('#histList').datagrid('getSelected');
    },
    reload: function () {
        $('#histList').datagrid('clearSelections').datagrid('reload', { filter: '' });
    }
};
function searchuser() {
    uname = $("#uname").val();
    getopt();
    $('#userGrid').datagrid('load', { filter: JSON.stringify(filterObj) });
}

function getopt() {
    var ruleArr = [];
    if (lv == 1) {
        sulevel = ulevel.toString().substr(0, 2);
        sulevel = "2," + sulevel;
        ruleArr.push({ "field": "ulevel", "op": "left", "data": sulevel });
    }
    else if (lv == 2) {
        sulevel = ulevel.toString().substr(0, 4);
        sulevel = "4," + sulevel;
        ruleArr.push({ "field": "ulevel", "op": "left", "data": sulevel });
    }
    else if (lv == 3) {
        sulevel = ulevel;
        ruleArr.push({ "field": "ulevel", "op": "eq", "data": sulevel });
    }
    if (uname != "") {
        ruleArr.push({ "field": "username", "op": "cn", "data": uname });
    }
    if (ruleArr.length > 0)
        filterObj = { groupOp: 'AND', rules: ruleArr };
    else
        filterObj = "";

}

var CRUD = {
    show: function () {
        var checkedItems = $('#histList').datagrid('getChecked');
        if (cid == "") {
            msg.error('对不起，请选择设备！');
            return;
        }
        if (checkedItems == "") {
            msg.error('对不起，请选择发送信息！');
            return;
        }
        hDialog = $("#wdialog").dialog({
            title: '发送信息', width: 380, height: 360,
            iconCls: '',
            onOpen: function () {
                $("#cc").combobox({
                    url: '/status_cbd/ashx/DataTestCBDHandler.ashx?Action=getUserRoleName',
                    valueField: 'KeyId',
                    textField: 'RoleName'
                });
                $("#cc").combobox({
                    onLoadSuccess: function (data) {
                        var selectData = $("#cc").combobox('getData');
                        if (selectData != null) {
                            $("#cc ").combobox('select', selectData[0].KeyId);
                        }

                    },
                    onSelect: function () {
                        $('#cct').combobox({
                            url: '/status_cbd/ashx/DataTestCBDHandler.ashx?Action=getUserInfo&roleId='
                                + $('#cc').combobox('getValue'),
                            valueField: 'UserID',
                            textField: 'TrueName',
                            multiple: "true"
                        });

                    }
                });

                var checkedStr = "";
                jQuery.each(checkedItems, function (i, d) {
                    devid = d.devID;
                    checkedStr += "设备ID：" + d.devID;
                    if (d.sta_ch0 == "0") {
                        checkedStr += "，灯管、";
                    }
                    if (d.sta_ch1 == "0") {
                        checkedStr += "，加热1、";
                    }
                    if (d.sta_ch2 == "0") {
                        checkedStr += "，加热2、";
                    }
                    if (d.sta_ch3 == "0") {
                        checkedStr += "，翻板、";
                    }
                    if (d.sta_ch4 == "0") {
                        checkedStr += "，接雨、";
                    }
                    if (d.sta_ch5 == "0") {
                        checkedStr += "，圆盘";
                    }
                    checkedStr += "工作不正常，更新时间：" + d.updateTime + ",";

                });

                checkedStr = checkedStr.substring(0, checkedStr.lastIndexOf(","));

                $('#sendcontent').val(checkedStr);
            },
            buttons: [{
                text: '确定',
                iconCls: 'icon-ok',
                handler: sendmsg
            }, {
                text: '关闭',
                iconCls: 'icon-cancel',
                handler: function () {
                    hDialog.dialog('close');
                }
            }]
        });
    }
};

function sendmsg() {

    var userids = $("#cct").combotree('getValues');
    var content = $("#sendcontent").val();
    if (userids == "") {
        msg.error("请选择客服人员！");
        return;
    }
    $('#sendfrm').form('submit', {
        url: '/status_cbd/ashx/DataTestCBDHandler.ashx',
        type: 'POST',
        onSubmit: function (param) {
            var isValid = $('#sendfrm').form('validate');
            if (isValid) {
                param.Action = "add";//这里一定要写上参数，否则找不到方法add
                param.uinfo = userids;
                param.cinfo = content;
                param.senduid = uid;
                param.equKeyId = devid;
                msg.ok('正在发送中...');
                return true;
            }
            else {
                return false;
            }
        },
        success: function (data) {
            var d = eval('(' + data + ')');
            if (d.Success) {                
                msg.ok('发送成功');
            }
            else {
                msg.error("发送失败");
            }
        }
    });



}

